<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>系统基础管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <el-row :gutter="10">
        <el-col span="4">
          <el-button
            type="primary"
            icon="el-icon-plus"
            @click="addDialogVisible = true"
            >新增</el-button
          >
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <el-table
        stripe
        border
        ref="multipleTable"
        :data="roleList"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column prop="name" label="角色名" width="340" align="center">
        </el-table-column>
        <el-table-column prop="state" label="状态" width="360" align="center">
          <template #default="scope">
            <span v-if="scope.row.state == 0">可用</span>
            <span v-else-if="scope.row.state == 1">禁用</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="roleKey"
          label="roleKey"
          width="360"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="description"
          label="描述"
          width="360"
          align="center"
        >
        </el-table-column>
        <el-table-column label="操作" width="180" align="center" fixed="right">
          <template #default="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="editDialog(scope.row)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="del(scope.row)"
            ></el-button>
            <el-button
              type="info"
              icon="el-icon-setting"
              size="mini"
              @click="distribution(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNow"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="rowCount"
      >
      </el-pagination>
    </el-card>
    <!-- 添加对话框 -->
    <el-dialog title="新增" v-model="addDialogVisible" width="40%">
      <!-- 对话框内容 -->
      <el-form
        :model="addForm"
        status-icon
        :rules="addFormrules"
        ref="addFormRef"
        label-width="135px"
      >
        <el-form-item label="角色名" prop="name">
          <el-input
            v-model="addForm.name"
            placeholder="请输入角色名"
          ></el-input>
        </el-form-item>
        <el-form-item label="rolekey" prop="roleKey">
          <el-input
            v-model="addForm.roleKey"
            placeholder="请输入rolekey"
          ></el-input>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input
            v-model="addForm.description"
            placeholder="请输入账号描述"
          ></el-input>
        </el-form-item>
        <el-form-item label="角色状态" prop="state">
          <el-select v-model="addForm.state" placeholder="请选择是否禁用">
            <el-option label="可用" value="0"></el-option>
            <el-option label="禁用" value="1"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="addDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="add">确 定</el-button>
        </span>
      </template>
    </el-dialog>
    <!-- 编辑对话框 -->
    <el-dialog title="编辑" v-model="editDialogVisible" width="40%">
      <!-- 对话框内容 -->
      <el-form
        :model="editForm"
        status-icon
        :rules="addFormrules"
        ref="editFormRef"
        label-width="135px"
      >
        <el-form-item label="角色名" prop="name">
          <el-input
            v-model="editForm.name"
            placeholder="请输入角色名"
          ></el-input>
        </el-form-item>
        <el-form-item label="rolekey" prop="roleKey">
          <el-input
            v-model="editForm.roleKey"
            placeholder="请输入rolekey"
          ></el-input>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input
            v-model="editForm.description"
            placeholder="请输入账号描述"
          ></el-input>
        </el-form-item>
        <el-form-item label="角色状态" prop="state">
          <el-select v-model="editForm.state" placeholder="请选择是否可用">
            <el-option label="可用" value="0"></el-option>
            <el-option label="禁用" value="1"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="editDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="edit">确 定</el-button>
        </span>
      </template>
    </el-dialog>
    <!-- 分配权限对话框 -->
    <el-dialog title="分配权限" v-model="distributionVisible" width="30%">
      <el-tree
        :data="treeList"
        show-checkbox
        :default-checked-keys = "dataList"
        node-key="id"
        ref="tree"
        highlight-current
        :props="defaultProps"
      >
      </el-tree>
      <el-button @click="getCheckedKeys"> 提交 </el-button>
      <el-button @click="distributionVisible = false"> 取消 </el-button>
    </el-dialog>
  </div>
</template>

<script>
import axios from "axios";
export default {
  data() {
    return {
      mealList: "",
      treeList:[],
      roleList: [],
      pageCount: 1,
      pageNow: 1,
      pageSize: 10,
      rowCount: 0,
      // 显示添加对话框
      addDialogVisible: false,
      // 显示编辑对话框
      editDialogVisible: false,
      // 显示分配权限对话框
      distributionVisible: false,
      dataList: [],
      defaultProps: {
        children: "children",
        label: "name",
      },
      roleId:'',
      addForm: {
        name: "",
        roleKey: "",
        description: "",
        state: "",
      },
      editForm: {},
      addFormrules: {
        name: [
          {
            required: true,
            message: "请输入角色名",
            trigger: "blur",
          },
        ],
        roleKey: [
          {
            required: true,
            message: "请输入rolekey",
            trigger: "blur",
          },
        ],
        description: [
          {
            required: true,
            message: "请输入账号描述",
            trigger: "blur",
          },
        ],
        state: [
          {
            required: true,
            message: "请选择角色状态",
            trigger: "blur",
          },
        ],
      },
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    getList() {
      let fromdata = new FormData();
      fromdata.append("pageSize", this.pageSize);
      fromdata.append("pageNow", this.pageNow);
      axios.post("/IE_MES/role/findByPage.shtml", fromdata).then((res) => {
        console.log(res.data.records);
        this.roleList = res.data.records;
        this.pageCount = res.data.pageCount;
        this.pageNow = res.data.pageNow;
        this.pageSize = res.data.pageSize;
        this.rowCount = res.data.rowCount;
      });
    },
    // 新增
    add() {
      this.$axios({
        headers: {
          "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
        },
        method: "post",
        url: "/IE_MES/role/addEntity.shtml",
        data: this.$qs.stringify({
          "roleFormMap.name": this.addForm.name,
          "roleFormMap.roleKey": this.addForm.roleKey,
          "roleFormMap.description": this.addForm.description,
          "roleFormMap.state": this.addForm.state,
        }),
      }).then((res) => {
        if (res.data == "success") {
          this.getList();
          this.$refs["addFormRef"].resetFields();
          this.addDialogVisible = false;
          this.$message({
            type: "success",
            message: "添加成功!",
          });
        }
      });
    },
    // 编辑
    editDialog(row) {
      this.editForm = JSON.parse(JSON.stringify(row));
      console.log(JSON.parse(JSON.stringify(row)));
      this.editDialogVisible = true;
    },
    edit() {
      this.$axios({
        headers: {
          "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
        },
        method: "post",
        url: "/IE_MES/role/editEntity.shtml",
        data: this.$qs.stringify({
          "roleFormMap.id": this.editForm.id,
          "roleFormMap.name": this.editForm.name,
          "roleFormMap.roleKey": this.editForm.roleKey,
          "roleFormMap.description": this.editForm.description,
          "roleFormMap.state": this.editForm.state,
        }),
      }).then((res) => {
        if (res.data == "success") {
          this.getList();
          this.editDialogVisible = false;
          this.$message({
            type: "success",
            message: "编辑成功!",
          });
        }
      });
    },
    // 删除
    del(row) {
      this.$confirm("此操作将永久删除该内容, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$axios({
            headers: {
              "Content-Type":
                "application/x-www-form-urlencoded; charset=UTF-8",
            },
            method: "post",
            url: "/IE_MES/role/deleteEntity.shtml?_t=" + new Date(),
            data: this.$qs.stringify({
              ids: row.id,
            }),
          }).then((res) => {
            if (res.data == "success") {
              this.getList();
              this.$message({
                type: "success",
                message: "删除成功!",
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 分配权限
    distribution(row) {
      this.distributionForm = JSON.parse(JSON.stringify(row));
      console.log(JSON.parse(JSON.stringify(row)));
      this.distributionVisible = true;
      this.roleId = row.id;
      let fromdata = new FormData();
      this.$axios.post("/IE_MES/resources/treelists.shtml", fromdata).then((res) => {
        console.log(res.data);
        this.treeList = res.data.treelists;
      });
      this.$axios({
        headers: {
          "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
        },
        method: "post",
        url: "/IE_MES/resources/findResRole.shtml",
        data: this.$qs.stringify({
          "resFormMap.roleId": row.id,
        }),
      }).then((res) => {
        this.mealList = res.data;
        console.log(res.data);
        this.dataList = this.mealList.map((item) => {
        return item.id});
        console.log(this.dataList);
      });
    },
    handleSizeChange(newSize) {
      this.pageSize = newSize;
      this.getList();
    },
    handleCurrentChange(newPage) {
      this.pageNow = newPage;
      this.getList();
    },
    getCheckedKeys() {
      console.log(this.$refs.tree.getCheckedKeys());
      this.$axios({
        headers: {
          "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
        },
        method: "post",
        url: "/IE_MES/resources/addUserRes.shtml",
        data:this.$qs.stringify({
          userId:'',
          roleId: this.roleId,
          resId:this.$refs.tree.getCheckedKeys().concat(this.$refs.tree.getHalfCheckedKeys())
        },{
          arrayFormat: 'brackets'
        })
      }).then((res) => {
        if (res.data == "success") {
          this.distributionVisible = false;
          this.$message({
            type: "success",
            message: "提交成功!",
          });
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.el-form-item /deep/ .el-input__inner {
  padding-right: 420px !important;
}
</style>
